/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.garagesale.web.form;

import com.garagesale.data.Item;
import com.garagesale.data.ItemCategory;
import com.garagesale.data.User;
import com.garagesale.web.util.WebAction;
import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import static com.garagesale.web.util.FormUtils.ensureExistence;
import static com.garagesale.web.util.FormUtils.ensureNonZeroExistence;

/**
 *
 * @author christophe
 */
public class ItemForm extends org.apache.struts.action.ActionForm
{

    private Integer itemID;
    private User seller;
    private User buyer;
    private int itemCategoryID;
    private String name;
    private String description;
    private double salePrice;
    private ItemCategory itemCategory;
    private String action;

    public void setAction(String action)
    {
        this.action = action;
    }

    public String getAction()
    {
        return action;
    }
    private List<ItemCategory> availableCategories;

    public User getBuyer()
    {
        return buyer;
    }

    public String getDescription()
    {
        return description;
    }

    public int getItemCategoryID()
    {
        return itemCategoryID;
    }

    public Integer getItemID()
    {
        return itemID;
    }

    public String getName()
    {
        return name;
    }

    public User getSeller()
    {
        return seller;
    }

    public void setBuyer(User buyer)
    {
        this.buyer = buyer;
    }

    public void setDescription(String description)
    {
        this.description = description;
    }

    public void setItemCategoryID(int itemCategoryID)
    {
        this.itemCategoryID = itemCategoryID;
    }

    public void setItemID(Integer itemId)
    {
        this.itemID = itemId;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public void setSeller(User seller)
    {
        this.seller = seller;
    }

    public void setAvailableCategories(List<ItemCategory> availableCategories)
    {
        this.availableCategories = availableCategories;
    }

    public void setSalePrice(double salePrice)
    {
        this.salePrice = salePrice;
    }

    public void fromItem(Item item)
    {
        this.buyer = item.getUserByBuyerUserId();
        this.seller = item.getUserBySellerUserId();
        this.itemID = item.getItemID();
        this.name = item.getName();
        this.description = item.getDescription();
        this.itemCategoryID = item.getItemCategory().getItemCategoryID();
        this.salePrice = item.getSalePrice().doubleValue();
        this.itemCategory = item.getItemCategory();
    }

    public List<ItemCategory> getAvailableCategories()
    {
        return availableCategories;
    }

    public double getSalePrice()
    {
        return salePrice;
    }

    public Item toItem()
    {
        Item item = new Item();
        item.setDescription(this.description);
        item.setName(this.name);
        item.setSalePrice(new BigDecimal(this.salePrice));
        item.setItemID(this.getItemID());

        return item;
    }

    public void setItemCategory(ItemCategory itemCategory)
    {
        this.itemCategory = itemCategory;
    }

    public ItemCategory getItemCategory()
    {
        return itemCategory;
    }

    /**
     *
     */
    public ItemForm()
    {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
    {
        ActionErrors errors = new ActionErrors();

         if (WebAction.DELETE.isAction(action))
        {
            ensureNonZeroExistence(itemID, "Item ID", errors);
        }
         else
         {
            ensureExistence(name, "name", errors);
            ensureExistence(description, "description", errors);
            ensureNonZeroExistence(salePrice, "Sale Price", errors);
            ensureNonZeroExistence(itemCategoryID, "Item Category", errors);

        }
        return errors;
    }
}
